package multigest.vocappulary;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class VocPracticeActivity extends Activity {

	
	private static String TAG_WORDS = "words";
	private static String TAG_ORIGINAL = "original";
	private static String TAG_TRANSLATED = "translated";
	
	private String catalogfile = null;
	private int word_cnt = 0;
	private boolean words_right[];
	private boolean show_all_words = false;
	
	private JSONObject cata_json = null;
	private JSONArray words = null;
	
	private TextView word_de = null;
	private TextView word_en = null;
	private ImageButton btn_apply = null;
	private ImageButton btn_deny = null;
	private RelativeLayout frame = null;
	

			
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_voc_training);

		catalogfile = getIntent().getStringExtra("catalog");
			 
		try {
			// Load JSON file
			cata_json = VocAppUtils.loadJSONFromAsset("Vocabulary/"+catalogfile, getAssets());	
			// Load words from JSON file
			words = cata_json.getJSONArray(TAG_WORDS);		
		} catch (JSONException e) {
           // AlertDialog instance
			Builder jsonErr = new AlertDialog.Builder(this);

           jsonErr.setTitle("JSON Error");
           jsonErr.setMessage("An error occurred while loading a vocabulary package!("+catalogfile+")");
           jsonErr.setNeutralButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
               	}
            });
           
            // Showing Alert Message
            jsonErr.show();			
            e.printStackTrace();
		}
		
		words_right = new boolean[words.length()];
				
        frame = (RelativeLayout)findViewById(R.id.trainingscreen);
        
		word_de = (TextView)findViewById(R.id.word_DE);
		word_en = (TextView)findViewById(R.id.word_EN);
		
		btn_apply = (ImageButton) findViewById(R.id.btn_apply);
		btn_deny = 	(ImageButton) findViewById(R.id.btn_deny);
		
	}

	
	@Override
	protected void onStart(){
		super.onStart();

        frame.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View view) {
				showWords(word_cnt);
			}
        });	
		
		showWords(word_cnt);
		
	}
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.voc_training, menu);
		return true;
	}

	
	private void showWords(int index){
		
		try {
			word_de.setText(words.getJSONObject(index).getString(TAG_ORIGINAL));
			
			if(show_all_words){
				word_en.setText(words.getJSONObject(index).getString(TAG_TRANSLATED));
				btn_apply.setVisibility(View.VISIBLE);
				btn_deny.setVisibility(View.VISIBLE);
			}else{
				word_en.setText(" ");
				btn_apply.setVisibility(View.GONE);
				btn_deny.setVisibility(View.GONE);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}	
		
		show_all_words = !show_all_words;
	}
	
	
	public void onResultButtonClick(View view){
		
		if(view.getId() == R.id.btn_apply){
			words_right[word_cnt] = true;
		}else{
			words_right[word_cnt] = false;
		}
		
		//Reset show all
		show_all_words = false;
		//Set counter to next word
		if(word_cnt < (words.length() -1) ){
			word_cnt++;
			
			//show new word
			showWords(word_cnt);
		}else{
			
	    	Intent intent = new Intent(getApplicationContext(), VocResultActivity.class);
           	intent.putExtra("new_result", VocAppUtils.getResult(words_right));
           	intent.putExtra("catalog", catalogfile);
	    	startActivity(intent);
		}
		
	}

}
	
	




